// components/grade-list/grade-list.js
const setting = require("../../http/env"); 
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    //默认域名
    baseUrl:setting.baseUrl,
    avatarUrl: wx.getStorageSync('userInfo').headimg ? (wx.getStorageSync('userInfo').headimg.includes("http") ? wx.getStorageSync('userInfo').headimg : setting.baseUrl + wx.getStorageSync('userInfo').headimg) : 'https://zsmatch.oss-cn-qingdao.aliyuncs.com/v3/20250327/headimg.png',
    ch_nick:wx.getStorageSync('userInfo').nickname,
  },

  /**
   * 组件的方法列
   */
  methods: {
    // 重新获取昵称
    bind_ch_nick(e) {
      var that = this;
      that.setData({
        ch_nick: e.detail.value,
      })
    },

    //重新选择头像
    onChooseAvatar(e) {
      var that=this;
      const { avatarUrl } = e.detail;
 
      wx.uploadFile({
        url: setting.baseUrl + '/index/upImg',
        header: {
          'content-type': 'multipart/form-data'
        },
        method: "POST",
        filePath:avatarUrl,
        name: 'img',
        formData: {
          'onlineid': that.data.user,
        },
        success: function(res) {
          //上传成功
          if (res.data != -1) {
						var img = res.data.replace(/[\r\n]/g,"").replace(/\ +/g, "");
            var imgUrl = img.includes("http") ? img : setting.baseUrl + img
            that.setData({
              avatarUrl: imgUrl,
            })
          }
        }
      });
    },
    closeNick() {
      this.comfirm();
      // this.triggerEvent("closeNick", true);
    },

    comfirm() {
      var that=this;
      httpApi
        .Post("/index/upWxNick", {
          user_id: wx.getStorageSync("userInfo").id,
          avatarUrl: that.data.avatarUrl,
          nickname:that.data.ch_nick,
        })
        .then((res) => {
          var user = wx.getStorageSync("userInfo") || {};
          wx.setStorageSync("userInfo", Object.assign({}, user, res.data));
          this.triggerEvent("closeNick", true);
        });
    },
  },
});
